package com.wisdomparking.tools;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wisdomparking.bean.Orders;
import com.wisdomparking.bean.Parking;
import com.wisdomparking.bean.VehicleInfo;
import com.wisdomparking.service.OrdersService;
import com.wisdomparking.service.ParkingService;
import com.wisdomparking.service.VehicleInfoService;

public class ToolService {
	
//	/**
//	 * 通过 App 支付完成后通知车场做抬杠动作【>】
//	 * 
//	 * @param identification	车场唯一标识
//	 * @param plateNumber		车牌号
//	 * @return
//	 */
//	public static boolean payfinishedSendParking(ParkingService parkingService, String identification, String plateNumber){
//		boolean flag = false;
//		
//		if(ToolValid.isNotEmpty(identification)){
//			Parking parking = parkingService.getParkingByIdentification(identification);
//			
//			if(ToolValid.isNotNull(parking)){
//				String queryString = "identification=" + identification + "&plateNumber=" + plateNumber;
//				ToolString.doGet(parking.getNoticeUrl(), queryString, "utf-8", true);
//				flag = true;
//			}
//		}
//		
//		return flag;
//	}
	
	
	
	/**
	 * 生成订单
	 * 
	 * @param ordersService
	 * @param vehicleInfoService
	 * @param parkingService
	 * @param plateNumber
	 * @param identification
	 * @param memberFlag
	 */
	public static Orders generateOrder(OrdersService ordersService, VehicleInfoService vehicleInfoService,
			ParkingService parkingService, String plateNumber, String identification, Boolean memberFlag) {
		Orders order = null;
		if (ToolValid.isNotEmpty(plateNumber) && ToolValid.isNotEmpty(identification)) {
			VehicleInfo vehicleInfo = vehicleInfoService.getVehicleInfoByPlateNumber(plateNumber);
			Parking parking = parkingService.getParkingByIdentification(identification);

			Date date = new Date();
			order = new Orders();

			if (ToolValid.isNotNull(vehicleInfo)) {
				order.setUserId(vehicleInfo.getUserId());
			} else {
				// 系统不存在车辆数据
				vehicleInfo = new VehicleInfo();
				vehicleInfo.setPlateNumber(plateNumber);
				vehicleInfo.setCreateTime(new Date());

				vehicleInfoService.addVehicleInfo(vehicleInfo, true);
			}
			order.setMemberFlag(memberFlag);
			order.setOrderNumber(ToolString.generateSingleNumber("JHZL_"));
			order.setParkingId(parking.getId());
			order.setParkingAddress(parking.getAddress());
			order.setParkingName(parking.getName());
			order.setStartTime(date);
			order.setIdentification(identification);
			order.setPlateNumber(plateNumber);
			order.setStatus(0);
			order.setCreateTime(date);

			ordersService.addOrders(order, true);
		}
		
		return order;
	}
	
	
	/**
	 * 结束订单
	 * @param ordersService
	 * @param plateNumber
	 * @param identification
	 * @param paymentAmount
	 * @param payMode
	 * @return
	 */
	public static boolean closureOrder(OrdersService ordersService, String plateNumber, String identification,
			String paymentAmount, String payMode) {
		boolean flag = false;

		if (ToolValid.isNotEmpty(plateNumber) && ToolValid.isNotEmpty(identification)) {
			Map<String, Object> m = new HashMap<>();
			m.put("plateNumber", plateNumber);
			m.put("statusLess", 2);
			m.put("orderByField", "id");
			m.put("orderByFlag", "desc");

			List<Orders> list = ordersService.getOrdersList(m);
			if (null != list && list.size() > 0) {
				Orders orders = list.get(0);
				if (ToolValid.isNotNull(orders)) {
					orders.setAmount(String.valueOf(Float.parseFloat(paymentAmount) * 100).split("\\.")[0]);
					orders.setPayMode(payMode);
					orders.setEndTime(new Date());
					orders.setStatus(2);

					flag = ordersService.updateOrders(orders, true);
				}
			}

		}

		return flag;
	}
	
	/**
	 * 查非本地注册的停车场
	 * 
	 * @param keyword
	 * @param region
	 * @param page_num
	 * @param page_size
	 * @return
	 */
	public static String getParkingFromJDWX(String keyword, String region, int page_num, int page_size) {
		String JDWXAppkey = "c00abfc1e31e5abc4c34c315f2bf2ca5";
		String url = "https://way.jd.com/xiuyoutech/getParking";
		System.out.println(url);
		String queryString = "?page_num="+page_num+"&page_size="+page_size+"&appkey="+JDWXAppkey+"&keyword=" + keyword + "&region=" + region;
		String content = ToolString.doGet(url, queryString, "utf-8", true);
		return content;
	}
	
	public static void main(String[] args) {
		String content = ToolService.getParkingFromJDWX("场", "北京", 5, 50);
		System.out.println(content);
	}
	
}
